package tq.com.tqstudent.net.ok;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.lib.common.utils.SPUtils;
import com.lib.common.utils.Tips;
import com.lib.common.views.LoadingDialog;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import org.json.JSONObject;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import tq.com.tqstudent.App;
import tq.com.tqstudent.bean.BaseBean;
import tq.com.tqstudent.bean.SimpleBaseBean;
import tq.com.tqstudent.bean.UserInfo;

/**
 * @Author rames
 * @date 2019/4/10 12:32
 */
public abstract class SimpleDialogCallback extends JsonCallback<SimpleBaseBean> {
    private Dialog dialog;
    private Context context;
    private boolean isShowDialog = true;

    private void initDialog(Activity activity) {
        this.context = activity;
        dialog = new LoadingDialog(context);
    }

    public SimpleDialogCallback(Activity activity) {
        super(SimpleBaseBean.class);
        this.context = activity;
        initDialog(activity);
    }

    public SimpleDialogCallback(Activity activity, boolean isShowDialog) {
        super(SimpleBaseBean.class);
        this.context = activity;
        this.isShowDialog = isShowDialog;
        if(isShowDialog) {
            initDialog(activity);
        }
    }

    @Override
    public void onStart(Request request) {
        super.onStart(request);
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    @Override
    public void onFinish() {
        //网络请求结束后关闭对话框
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void onError(Response response) {
        //网络请求结束后关闭对话框
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }

        SimpleBaseBean bean = new SimpleBaseBean();
        bean.ret = Integer.MIN_VALUE;
        bean.msg = response!=null ? response.message() : "请求失败";
        handleFail(bean);
    }

    @Override
    public void onSuccess(Response response) {
        try {
            SimpleBaseBean bean = (SimpleBaseBean) response.body();
            if(bean.isSuccess()){
                handleSuccess(bean);
            } else {
                handleFail(bean);
            }
        }catch (Exception e){
            handleFail(null);
            e.printStackTrace();
        }
    }

    public abstract void handleSuccess(SimpleBaseBean data);

    public void handleFail(SimpleBaseBean body){
        if(null == this.context){
            context = App.getApp().getApplicationContext();
        }

        if(null == body) {
            Tips.showTips(context,"请求失败");
            return;
        }

        if(!TextUtils.isEmpty(body.getMsg())){
            Tips.showTips(context, body.getMsg());
            return;
        }
    }
}
